package com.ruoyi.cscec.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.cscec.domain.Equipment;
import com.ruoyi.cscec.service.IEquipmentService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 设备管理Controller
 *
 * @author ruoyi
 * @date 2021-09-26
 */
@RestController
@RequestMapping("/equipment")
public class EquipmentController extends BaseController
{
    @Autowired
    private IEquipmentService equipmentService;

    /**
     * 查询设备管理列表
     */
    @PreAuthorize(hasPermi = "cscec:equipment:list")
    @GetMapping("/list")
    public TableDataInfo list(Equipment equipment)
    {
        startPage();
        List<Equipment> list = equipmentService.selectEquipmentList(equipment);
        return getDataTable(list);
    }

    /**
     * 导出设备管理列表
     */
    @PreAuthorize(hasPermi = "cscec:equipment:export")
    @Log(title = "设备管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Equipment equipment) throws IOException
    {
        List<Equipment> list = equipmentService.selectEquipmentList(equipment);
        ExcelUtil<Equipment> util = new ExcelUtil<Equipment>(Equipment.class);
        util.exportExcel(response, list, "设备管理数据");
    }

    /**
     * 获取设备管理详细信息
     */
    @PreAuthorize(hasPermi = "cscec:equipment:query")
    @GetMapping(value = "/{eqId}")
    public AjaxResult getInfo(@PathVariable("eqId") Long eqId)
    {
        return AjaxResult.success(equipmentService.selectEquipmentByEqId(eqId));
    }

    /**
     * 新增设备管理
     */
    @PreAuthorize(hasPermi = "cscec:equipment:add")
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Equipment equipment)
    {
        return toAjax(equipmentService.insertEquipment(equipment));
    }

    /**
     * 修改设备管理
     */
    @PreAuthorize(hasPermi = "cscec:equipment:edit")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Equipment equipment)
    {
        return toAjax(equipmentService.updateEquipment(equipment));
    }

    /**
     * 删除设备管理
     */
    @PreAuthorize(hasPermi = "cscec:equipment:remove")
    @Log(title = "设备管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{eqIds}")
    public AjaxResult remove(@PathVariable Long[] eqIds)
    {
        return toAjax(equipmentService.deleteEquipmentByEqIds(eqIds));
    }

    /**
     * 状态修改
     */
    @PreAuthorize(hasPermi = "cscec:equipment:edit")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody Equipment equipment)
    {
        equipment.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(equipmentService.updateEquipmentStatus(equipment));
    }

    /**
     * 获取设备管理详细信息
     */
    @PreAuthorize(hasPermi = "cscec:equipment:query")
    @GetMapping(value = "/flowAndRepair/{eqId}")
    public AjaxResult getFlowAndRepairInfo(@PathVariable("eqId") Long eqId)
    {
        AjaxResult success = AjaxResult.success(equipmentService.getFlowAndRepairInfo(eqId));
        return success;
    }


}
